package com.gipson.amc.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dsbj.common.msg.DataPageMessage;
import com.gipson.amc.entity.*;
import com.gipson.amc.model.TreeNode;
import com.gipson.amc.service.basicdata.AuthorityService;
import com.gipson.amc.service.basicdata.CompanyService;
import com.gipson.amc.service.basicdata.PostGroupService;
import com.gipson.amc.utils.Constants;
import com.gipson.amc.utils.security.ShiroUtils;
import com.gipson.amc.utils.strprocess.CompareUtil;
import com.gipson.amc.utils.strprocess.ObjectParse;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.entity.PostGroup;
import com.gipson.amc.service.basicdata.UserPostGroupService;
import com.gipson.amc.utils.strprocess.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 岗位管理 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2020-05-21
 */
@Controller
@RequestMapping("/postGroup")
public class PostGroupController extends BaseController {
	
	public static String viewPrefix = "/basicdata/postGroup";
	
	@Resource
	private CompanyService companyService;
	
	@Resource
	private PostGroupService postGroupService;
	
	@Resource
	private UserPostGroupService userPostGroupService;
	
	
	/**
	 * @param
	 * @return
	 * @author cjw 2020-03-23
	 */
	@RequestMapping(value = "/list")
	public String list(ModelMap modelMap) {
		User user = ShiroUtils.getCurrentUser();
		//StrUtil.prettyPrint("user", user);
		
		//获取公司列表
		List<Company> companyList = companyService.list();
		modelMap.put("companyList", companyList);
		//获取默认company
		Company companyDefault = companyService.getCompanyByCode(user.getCompanyCode());
		modelMap.put("companyDefault", companyDefault);
		return viewPrefix + "/list";
	}
	
	/**
	 * datatable展示》获取"树状结构"的岗位列表。
	 * 传入参数: companyCode,postNameDim
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-24
	 */
	@RequestMapping(value = "/getPostGroupByPage")
	@ResponseBody
	public DataPageMessage getPostGroupByPage(PageByDataTable pageByDataTable, PostGroup postGroup) {
		//解析传来的参数
		if (StrUtil.isEmpty(postGroup.getCompanyCode())) {
			return DataPageMessage.error("companyCode为空!");
		}
		
		//根据postNameDim, companyCode模糊查询postGroup列表。
		List<PostGroup> postGroupList = postGroupService.getPostGroupByPostNameDimByCompanyCode(postGroup.getPostNameDim(),
				postGroup.getCompanyCode());
		JSONArray jsonArray = PostGroupService.generatePostGroup(postGroupList);
		return DataPageMessage.ok(jsonArray, jsonArray.size(), jsonArray.size(), pageByDataTable.getSEcho(), "");
	}
	
	/*****
	 * 分配用户wcm
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("editUser")
	public String editUser(Model model, String id) {
		PostGroup postGroup = postGroupService.getById(id);
		model.addAttribute("postGroup", postGroup);
		return "basicdata/postGroup/editUser";
	}
	
	/*******
	 * 用户岗位匹配分页wcm
	 * @param userPostGroup
	 * @param pageByDataTable
	 * @return
	 */
	@RequestMapping("ajaxUserPostGroupPage")
	@ResponseBody
	public DataPageMessage ajaxLoadUserRoleData(UserPostGroup userPostGroup, PageByDataTable pageByDataTable) {
		IPage<UserPostGroup> page = userPostGroupService.selectPageForUserPostGroup(userPostGroup, pageByDataTable);
		return DataPageMessage.ok(page.getRecords(), (int) page.getTotal(), (int) page.getTotal(), pageByDataTable.getSEcho(), "");
	}
	
	/*****
	 * 分配用户htmlwcm
	 * @return
	 */
	@RequestMapping("selectUser")
	public String selectUser(Model model, String postGroupId, String companyCode) {
		model.addAttribute("postGroupId", postGroupId);
		model.addAttribute("companyCode", companyCode);
		return "basicdata/postGroup/selectUser";
	}
	
	/*******
	 * 保存用户岗位wcm
	 * @param postGroupId
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/saveUserPost")
	@ResponseBody
	public ResponseMessage saveUserPost(String postGroupId, String userIds) {
		return userPostGroupService.saveUserPost(postGroupId, userIds);
	}
	
	/********
	 * 删除用户岗位对应表wcm
	 * @param userId
	 * @param postGroupId
	 * @return
	 */
	@RequestMapping("/deleteUserPostGroup")
	@ResponseBody
	public ResponseMessage deleteUserPostGroup(String userId, String postGroupId) {
		return userPostGroupService.deleteUserPostGroup(userId, postGroupId);
	}
	
	/**
	 * 新增、修改页面。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	@RequestMapping(value = "/edit")
	public String edit(String companyCode, String id, ModelMap modelMap) {
		
		/**
		 * 根据id判断该页面的类型("新增页面"、"修改页面")。
		 */
		//若id为空,该页面为"新增页面"。
		if (StrUtil.isEmpty(id)) {
			//companyCode
			if (StrUtil.isEmpty(companyCode)) {
				modelMap.put("errMsg", "companyCode为null!");
				return Constants.ERROR_PAGE;
			}
			//获取company
			Company company = companyService.getCompanyByCode(companyCode);
			//将company.companyCode,companyName存入postGroup中。
			PostGroup postGroup = new PostGroup();
			postGroup.setCompanyCode(company.getCompanyCode());
			postGroup.setCompanyName(company.getCompanyName());
			modelMap.put("postGroup", postGroup);
		} else {
			//若id不为空,返回"修改页面"。
			PostGroup postGroup = postGroupService.getPostGroupFullById(id);
			modelMap.put("postGroup", postGroup);
		}
		return viewPrefix + "/edit";
	}
	
	/**
	 * ztree展示》获取岗位树。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/getPostGroupTree")
	@ResponseBody
	public ResponseMessage getPostGroupTree(String companyCode) {
		//解析传来的参数
		if (StrUtil.isEmpty(companyCode)) {
			return ResponseMessage.error("companyCode为空!");
		}
		
		/**
		 * 获取岗位列表,并以"岗位树格式"输出。
		 */
		List<PostGroup> postGroupList = postGroupService.getPostGroupByCompanyCode(companyCode);
		List<TreeNode> treeNodeList = new ArrayList<>();
		TreeNode treeNode = null;
		
		for (PostGroup postGroup : postGroupList) {
			treeNode = new TreeNode();
			treeNode.setId(postGroup.getId());
			treeNode.setIcon("/static/imgs/tree/material.png");
			treeNode.setPId(postGroup.getParentId());
			treeNode.setName(postGroup.getPostName());
			treeNode.setValue(postGroup.getPostCode());
			treeNodeList.add(treeNode);
		}
		return ResponseMessage.ok(treeNodeList);
	}
	
	/**
	 * 存入postGroup信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/addPostGroup")
	@ResponseBody
	public ResponseMessage addPostGroup(PostGroup postGroup) {
		return postGroupService.addPostGroup(postGroup);
	}
	
	/**
	 * 更新postGroup信息。
	 * 传入参数id,
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/updatePostGroup")
	@ResponseBody
	public ResponseMessage updatePostGroup(PostGroup postGroup) {
		return postGroupService.updatePostGroup(postGroup);
	}
	
	/**
	 * 根据id删除postGroup, 若该postGroup下含有子岗位,会被一并删除(该方法为物理删除)。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-08
	 */
	@RequestMapping(value = "/deletePostGroup")
	@ResponseBody
	public ResponseMessage deletePostGroup(String id) {
		//解析传来的参数
		if (StrUtil.isEmpty(id)) {
			return ResponseMessage.error("id为空!");
		}
		
		//根据id删除postGroup, 若该postGroup下含有子岗位,会被一并删除(该方法为物理删除)。
		ResponseMessage responseMessageTemp = postGroupService.deletePostGroup(id);
		return responseMessageTemp;
	}
	
	/**
	 * 新增下级岗位页面。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-08
	 */
	@RequestMapping(value = "/addSubPostGroup")
	public String addSubPostGroup(String id, ModelMap modelMap) {
		//解析传来的参数
		if (StrUtil.isEmpty(id)) {
			modelMap.put("errMsg", "id为空!");
			return Constants.ERROR_PAGE;
		}
		
		//根据id获取postGroup信息。
		PostGroup postGroup = postGroupService.getPostGroupFullById(id);
		if (postGroup == null) {
			modelMap.put("errMsg", "未找到该id对应的postGroup信息!id=" + id);
			return Constants.ERROR_PAGE;
		}
		//将postGroup.companyCode,companyName等信息存入postGroup2中。
		PostGroup postGroup2 = new PostGroup();
		postGroup2.setCompanyCode(postGroup.getCompanyCode());
		postGroup2.setCompanyName(postGroup.getCompanyName());
		postGroup2.setParentId(postGroup.getId());
		postGroup2.setParentPostCode(postGroup.getPostCode());
		postGroup2.setParentPostName(postGroup.getPostName());
		modelMap.put("postGroup", postGroup2);
		return viewPrefix + "/edit";
	}
	
	
}
